package com.focus.xyz.union.pages.frontend.tables;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseTable;
import com.focus.xyz.core.utils.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/4/21.
 */
public class ProdInfoTable extends BaseTable {
    @FindBy(xpath = "//table[@class='tab-prod-info']/tbody")
    private WebElement table;
    private static final String ROW_XPATH = "tr[2]/td/ul";


    public ProdInfoTable(WebDriver driver,Action action) {
        super(action);
        PageFactory.initElements(driver,this);
    }

    @Override
    protected By getLinkLocator() throws Exception {
        return null;
    }

    @Override
    protected By getLocator(int index) throws Exception {
        return null;
    }

    private WebElement getPeriodRow() throws Exception{
        return table.findElement(By.xpath(ROW_XPATH));
    }

    public void selectPeriod(String key) throws Exception{
        List<WebElement> list = getPeriodRow().findElements(By.tagName(Constants.TAG_LI));
        for(WebElement e : list){
            if(key.equalsIgnoreCase(e.getText())){
                action.click(e);
                break;
            }else{
                if(e.getText().contains("-")){
                    int keyTime = Integer.parseInt(key.substring(0,key.length()-1));
                    String targetTime = e.getText().trim().substring(0,e.getText().trim().length()-1);
                    String[] tmp = targetTime.split("-");
                    int[] tmp1 = new int[tmp.length];
                    for(int i=0;i<tmp.length;i++){
                        tmp1[i]=Integer.parseInt(tmp[i]);
                    }
                    if(keyTime >= tmp1[0]&&keyTime <= tmp1[1]){
                        action.click(e);
                        break;
                    }
                }
            }
        }
    }
}
